package sdk.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lgq.service.CertificateManager;
import model.Contract;
import model.Emp;
import model.RewAndPun;
import model.RewAndPunType;
import sdk.service.ContractManager;
import sdk.service.RewAndPunManager;
import util.Page;

public class FindRewAndPunServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			this.doPost(request, response);
		
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		int pageNo=1;
		int pageSize=2;
		String keycode="";
		int idkeycode=0;
		Date starttime=null;
		Date endtime=null;

		
		if (null != request.getParameter("serchtype")) {
			request.getSession().setAttribute("serchtype",request.getParameter("serchtype"));
		}
		
		if (null != request.getParameter("keycode")) {
			request.getSession().setAttribute("keycode",request.getParameter("keycode"));
			keycode = request.getParameter("keycode");
		} else {
			keycode = (String) request.getSession().getAttribute("keycode");
		}
		try{
			if(request.getParameter("pageNo")!=null)
			{
				pageNo=Integer.parseInt(request.getParameter("pageNo"));
			}
			if(request.getParameter("pageSize")!=null)
			{
				pageSize=Integer.parseInt(request.getParameter("pageSize"));
				
				request.getSession().setAttribute("pageSize", pageSize);
			}
			if(request.getSession().getAttribute("pageSize")!=null)
			{
				pageSize=(Integer) request.getSession().getAttribute("pageSize");
			}
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		Page<RewAndPun> page=null;
		
		
	 if("Content".equals(request.getSession().getAttribute("serchtype")))
		{
			page=new RewAndPunManager().getByEmpId(pageNo, pageSize,idkeycode);				
		}
	  else if("rewAndPunType".equals(request.getSession().getAttribute("serchtype")))
		{
		  RewAndPunType rapt = new RewAndPunType();
		 rapt.setId(idkeycode);
		 rapt.setType(keycode);
			page=new RewAndPunManager().getByRewAndPunType(pageNo, pageSize,rapt);				
		}

	  else if("time".equals(request.getSession().getAttribute("serchtype")))
		{		
			if (null != request.getParameter("starttime")) {
				
				try {
					request.getSession().setAttribute("starttime",new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("starttime")));
					starttime = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("starttime"));
				} catch (ParseException e) {						
					e.printStackTrace();
				}					
			} else {
					starttime = (Date) request.getSession().getAttribute("starttime");
			}
			
			if (null != request.getParameter("endtime")) {
				
				try {
					request.getSession().setAttribute("endtime",new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("endtime")));
					endtime = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("endtime"));
				} catch (ParseException e) {						
					e.printStackTrace();
				}					
			} else {
				endtime = (Date) request.getSession().getAttribute("endtime");
			}

			page=new RewAndPunManager().getByTime(pageNo, pageSize, starttime, endtime);
		}

		else if("Content".equals(request.getSession().getAttribute("serchtype")))
		{
			page=new RewAndPunManager().getByContent(pageNo, pageSize,keycode);				
		}

		else
		{			
			page=new RewAndPunManager().getAll(pageNo, pageSize);
		}
		request.setAttribute("page", page);
		request.getRequestDispatcher("/Dossier/Rewandpun.jsp").forward(request, response);
	}

}
